//2016-6-13
var net = require('net');
var server = net.createServer();
var time = 0;
server.on('connection',(socket) => {
    time = time + 1;
    socket.write('Hello');
    console.log('Bien connection '+time+' time');
    // if lose data, despite of data length, how could we know the correction. 
    socket.on('data',(data)=>{
        console.log('Received ',socket.bytesRead,' bytes data');
   
    //test bytes stream
        // data.forEach((d,index)=>{
        //     console.log(index+'\'s value is '+d);
        // });

        //var crossID
        var str_crossid = new String();
        for(var i = 0; i< 8; i++){
            str_crossid = str_crossid+String.fromCharCode(data[i]);
        }
        console.log('-Cross ID: ',str_crossid);

        //Interval
        var interval = data[14]+data[15]*256;
        console.log('-Interval: ',interval);

        //var DeviceType
        var device_type = new String();
        device_type = String.fromCharCode(data[13]);
        console.log('-Device Type: ',device_type);

        //var LaneNo
        var lane_no = new String();
        for(var i = 16; i < 19; i++){
            lane_no  = lane_no + String.fromCharCode(data[i])
        }
        console.log('-Lane Nomber: ',lane_no);
    
        //var DateTime
        var str_date_time = new String();
        for(var i = 19; i < 38; i++){
            str_date_time  = str_date_time + String.fromCharCode(data[i]);
        }
        var date_time = new Date(str_date_time);
        console.log('-Date Time(ISO standard)：',date_time.toString());
        console.log(' Date Time(String): ',str_date_time);

        //var Volume
        var volume = data[39]+data[40]*256;
        console.log('-Volume: ',volume);

        //var Volume1                   
        var volume1 = data[41]+data[42]*256;
        console.log('-Volume1: ',volume1);
        
        //var Volume2
        var volume2 = data[43]+data[44]*256;
        console.log('-Volume2: ',volume2);
        
        //var Volume3
        var volume3 = data[45]+data[46]*256;
        console.log('-Volume3: ',volume3);
        
        //var Volume4
        var volume4 = data[47]+data[48]*256;
        console.log('-Volume4: ',volume4);
       
        //var Volume5
        var volume5 = data[49]+data[50]*256;
        console.log('-Volume5: ',volume5);

        //var AveOccupancy
        var ave_occup = data[51] + data[52]*256;
        console.log('-AveOccupany: ',ave_occup);

        //var AveHeaderTime
        var ave_head = data[53] + data[54]*256;
        console.log('-AveHeaderTime: ',ave_head);

        //var Avelength
        var buf_avg_l = new Buffer(4);
        for(var i = 55; i <= 58; i++){
            buf_avg_l[i-55] = data[i];
        }
        console.log('-AveLength: '+ buf_avg_l.readFloatLE(0));

        //var  AveSpeed
       var buf_avg_s = new Buffer(4);
        for(var i = 59; i <= 62; i++){
            buf_avg_s[i-59] = data[i];
        }
        console.log('-AveSpeed: '+buf_avg_s.readFloatLE(0));

        //var Saturation
        var saturation = data[63];
        console.log('-Saturation: ',saturation);

        //var Density
        var density = data[64] + data[65]*256;
        console.log('-Density: ',density);

        //var Pcu
        var pcu = data[66] + data[67]*256;      
        console.log('-pcu: ',pcu);

        //var AvgQueueLength
        var buf_ave_ql = new Buffer(4);
        for(var i = 68; i <= 71; i++){
            buf_ave_ql[i-68] = data[i];
        }
        console.log('-AveQueueLength: '+ buf_ave_ql.readFloatLE(0));

        //var ManufacturerCode	    
        var manu_code = data[72];
        console.log('-ManufacturerCode: '+ manu_code);

        //var CheckCode
        var check_code = data[73];
        console.log('-CheckCode: '+ check_code);

    });
    
});
server.listen(4001);
console.log('Server started,listening port 4001:...');